package com.xuelang.pipeline.app.pipeline.impl;

import cn.hutool.core.util.IdUtil;
import com.xuelang.hiforce.base.tracing.TracingUtils;
import com.xuelang.pipeline.app.pipeline.TagService;
import com.xuelang.pipeline.app.pipeline.converter.TagDTOToTagConverter;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.common.util.BizAssert;
import com.xuelang.pipeline.client.pipeline.dto.PipelineTagQuery;
import com.xuelang.pipeline.client.pipeline.dto.TagDTO;
import com.xuelang.pipeline.client.pipeline.dto.TagQuery;
import com.xuelang.pipeline.domain.pipeline.Tag;
import com.xuelang.pipeline.domain.pipeline.service.PipelineTagDomainService;
import com.xuelang.pipeline.domain.pipeline.service.TagDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 流水线标签服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    TagDomainService tagDomainService;

    @Autowired
    PipelineTagDomainService pipelineTagDomainService;

    @Override
    public PageRecord<TagDTO> pageList(PageParam<TagQuery> dtoPageParam) {
        TagQuery condition = dtoPageParam.getCondition();
        condition = condition == null ? new TagQuery() : condition;
        PageRecord<Tag> pageRecord = tagDomainService.pageList(
                dtoPageParam.getCurrentPage(),
                dtoPageParam.getPageSize(),
                condition);
        return new PageRecord<>(
                pageRecord.getCurrentPage(),
                pageRecord.getPageSize(),
                pageRecord.getTotalCount(),
                pageRecord.getTotalPages(),
                pageRecord.getRecords().stream().map(TagDTOToTagConverter.INSTANCE::tagToTagDTO).collect(Collectors.toList()));
    }

    @Override
    public List<TagDTO> list(TagQuery tagQuery) {
        return tagDomainService.list(tagQuery).stream().map(TagDTOToTagConverter.INSTANCE::tagToTagDTO).collect(Collectors.toList());
    }

    @Override
    public Boolean add(TagDTO tagDTO) {
        BizAssert.notNull(tagDTO.getName(), "name不能为空");
        Tag tag = TagDTOToTagConverter.INSTANCE.tagDTOToTag(tagDTO);

        tag.setUuid(IdUtil.fastSimpleUUID());
        return tagDomainService.save(tag);
    }

    @Override
    public Boolean update(TagDTO tagDTO) {
        BizAssert.notNull(tagDTO.getUuid(), "uuid不能为空");
        BizAssert.notNull(tagDTO.getName(), "name不能为空");
        Tag tag = TagDTOToTagConverter.INSTANCE.tagDTOToTag(tagDTO);
        return tagDomainService.update(tag);
    }

    @Override
    public Boolean batchSaveOrUpdate(List<TagDTO> tagDTOList) {
        BizAssert.notEmpty(tagDTOList, "批量添加标签的数组不能为空");
        List<Tag> tagList = tagDTOList.stream()
                .map(TagDTOToTagConverter.INSTANCE::tagDTOToTag).collect(Collectors.toList());

        return tagDomainService.batchSaveOrUpdate(tagList, TracingUtils.getUserId(), new Date());
    }

    @Override
    public Boolean delete(String uuid) {
        BizAssert.notNull(uuid, "uuid不能为空");
        return tagDomainService.delete(uuid);
    }

    @Override
    public TagDTO query(String uuid) {
        BizAssert.notNull(uuid, "uuid不能为空");
        return TagDTOToTagConverter.INSTANCE.tagToTagDTO(tagDomainService.query(uuid));
    }

    @Override
    public Boolean modifyPipelineTags(PipelineTagQuery query) {
        return pipelineTagDomainService.modifyPipelineTags(query, TracingUtils.getUserId());
    }

}
